<!-- delete confirmation dialog box -->
<div id="delConfDialog" title="Confirm">
    <p>Anda yakin?</p>
</div>
<!-- message dialog box -->
<div id="msgDialog"><p></p></div>
<!-- edited by : alfin akhret
SSP datattables -->
<table id="records" class="handsetRecord">
    <thead>
    </thead>
    <tbody>
    </tbody>
</table>
<!--  -------------->

<script type="text/javascript">
var readUrl   = 'index.php/product/getPricesData';
  

$( function() {
    
    readUsers();
    $( '#msgDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'Ok': function() {
                $( this ).dialog( 'close' );
            }
        }
    });
    
    $( '#delConfDialog' ).dialog({
        autoOpen: false,
        buttons: {
            'No': function() {
                $( this ).dialog( 'close' );
            },
            'Yes': function() {
                //display ajax loader animation here...
                $( '#ajaxLoadAni' ).fadeIn( 'slow' );
                $( this ).dialog( 'close' );
                $.ajax({
                    url: delHref,
                    success: function( response ) {
                        //hide ajax loader animation here...
                        $( '#ajaxLoadAni' ).fadeOut( 'slow' );
                        
                        $( '#msgDialog > p' ).html( response );
                        $( '#msgDialog' ).dialog( 'option', 'title', 'Success' ).dialog( 'open' );
                        
                        $( 'a[href=' + delHref + ']' ).parents( 'tr' )
                        .fadeOut( 'slow', function() {
                            $( this ).remove();
                        });
                        
                    } //end success
                });
            } //end Yes
        } //end buttons
    }); //end dialog
    
    
    $( '#records' ).delegate( 'a.deleteBtn', 'click', function() {
        delHref = $( this ).attr( 'href' );
        $( '#delConfDialog' ).dialog( 'open' );
            return false;
    }); //end delete delegate
    // --- Create Record with Validation ---
}); //end document ready


// function readUsers() {
//     //display ajax loader animation
//     $( '#ajaxLoadAni' ).fadeIn( 'slow' );
//     var j=0;
//     $.ajax({
//         url: readUrl,
//         dataType: 'json',
//         success: function( response ) {
//            for( var i in response ) {
//                 response[ i ].nomor = j;
//                 j++;
//             }
//             //clear old rows
//             $( '#records > tbody' ).html( '' );
            
//             //append new rows
//             $( '#readTemplate' ).render( response ).appendTo( "#records > tbody" );
            
//             //apply dataTable to #records table and save its object in dataTable variable
//             if( typeof dataTable == 'undefined' )
//                 dataTable = $( '#records' ).dataTable({"bJQueryUI": true});
            
//             //hide ajax loader animation here...
//             $( '#ajaxLoadAni' ).fadeOut( 'slow' );
//         }
//     });
// } // end readUsers

function readUsers() {
    $('.handsetRecord').dataTable({
                "bJQueryUI": true, // aktifkan tampilan tabel JQueryUI
                "sAjaxSource": readUrl, //data source
                "sAjaxDataProp": "aaData", //variabel yg menampung data table
                "bServerSide": true, //serverside processing true
                "bProcessing": true, // tampilkan 'processing...' status saat data dipanggil
                //"sPaginationType": 'fullNumbers',
                "bPaginate": true,
                "aoColumns":[ //tentukan kolom pd tabel yg akan menampung value dari server
                    {"mDataProp": "item_name", "sTitle": "Nama"}, 
                    {"mDataProp": "item_code", "sTitle": "Kode Barang"}, 
                    {"mDataProp": "user_group_name", "sTitle": "Group"}, 
                    {"mDataProp": "pricing_date", "sTitle": "Tanggal"}, 
                    {"mDataProp": "retail_price", "sTitle": "Harga"},
                    {"mDataProp": "condition_type", "sTitle": "Tipe Kondisi"},
                    {"mDataProp": "condition_value", "sTitle": "Harga Baru"},
                    {"mDataProp": "selling_type", "sTitle": "Tipe Penjualan"}
                    // {"mDataProp": "shipment_date", "sTitle": "Tanggal Pengiriman"},
                    // {"mDataProp": "istatus", "sTitle": "Status Barang", "bSearchable":false},
                    
                ],
                "fnRowCallback": function(nRow, aaData, iDisplayIndex, iDisplayIndexFull) 
                {
                    //selling type 

                    var selling_type = $(nRow).find('td').eq(-1).html();
                    if(selling_type == '1')
                    {
                        $(nRow).find('td').eq(-1).html('Agent');
                    }
                    else if(selling_type == '2')
                    {
                        $(nRow).find('td').eq(-1).html('Mobile Selling');
                    }
                    else
                    {
                        $(nRow).find('td').eq(-1).html('-');
                    }

                    //condition type
                    var condition_type = $(nRow).find('td').eq(-3).html();
                    if(condition_type == '1')
                    {
                        $(nRow).find('td').eq(-3).html('New Price');
                        var harga = $(nRow).find('td').eq(-2).html();
                        $(nRow).find('td').eq(-2).html('Rp '+format_number(harga));
                    }
                    else if(condition_type == '2')
                    {
                        $(nRow).find('td').eq(-3).html('Discount');
                        var harga = $(nRow).find('td').eq(4).html();
                        var discount = $(nRow).find('td').eq(6).html(); 
                        var potongan_harga = harga*(discount/100);
                        var harga_baru = harga - potongan_harga;
                        $(nRow).find('td').eq(-2).html('Rp '+format_number(harga_baru));
                    }

                    return nRow;
                }
        });

function format_number(price)
{
    price += '';
    x = price.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2 + ",00";
}
}
</script>
